package samsung.sds.systemMonitor.userOperationMonitor.model;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import samsung.sds.system.utils.CommonTools;
import nl.bitwalker.useragentutils.UserAgent;

/**
 * 获得登录用户的基本信息
 * 
 * @author lixin.qiu
 * @createDate 2016-02-22
 * 
 */
public class MonitorUserBaseInfo {

	/**
	 * 获得主机
	 * 
	 * @param request
	 * @return
	 * @throws UnknownHostException
	 */
	public static String getHostName() throws UnknownHostException {
		InetAddress netAddress = InetAddress.getLocalHost();
		if (null == netAddress) {
			return null;
		}
		String name = netAddress.getHostName();
		return name;
	}

	/**
	 * 得到用户的浏览器名
	 * 
	 * @param request
	 * @return
	 */
	public static String getBrowserName(HttpServletRequest request) {
		String header = request.getHeader("User-Agent");
		if (header.indexOf("rv:11.0") > -1) {
			return "IE11";
		}
		UserAgent browser = UserAgent.parseUserAgentString(header);
		String userbrowser = browser.getBrowser().getName();
		return userbrowser;
	}

	/**
	 * 获得操作者的基本信息
	 * 
	 * @param request
	 * @return
	 * @throws UnknownHostException
	 */
	public static MonitorOperationEntity getMonitorOperationInfo(
			HttpServletRequest request) throws UnknownHostException {
		MonitorOperationEntity monitorOperationEntity = new MonitorOperationEntity();
		monitorOperationEntity.setId(CommonTools.getKeyUUID());
		monitorOperationEntity.setMacAddr("00-50-56-C0-00-08");
		monitorOperationEntity.setExplorerName(getBrowserName(request));
		monitorOperationEntity.setHostName(getHostName());
		monitorOperationEntity.setModuleName("");
		monitorOperationEntity.setCreateTime(CommonTools.getCurrentDateTime());
		monitorOperationEntity.setIp(request.getRemoteAddr());
		return monitorOperationEntity;
	}
}
